package com.coincalf.service.impl;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import com.coincalf.dto.ForexOpenPositionOrderDTO;
import com.coincalf.dto.FuForexOpenPositionOrderDTOMapper;
import com.coincalf.entity.ForexOpenPositionOrder;
import com.coincalf.entity.Market;
import com.coincalf.entity.TurnoverOrder;
import com.coincalf.framework.dto.ForexMarketDTO;
import com.coincalf.framework.enums.OpenPositionOrderStatus;
import com.coincalf.framework.enums.OrderType;
import com.coincalf.mapper.ForexOpenPositionOrderMapper;
import com.coincalf.service.ForexOpenPositionOrderService;
import com.coincalf.service.MarketService;
import com.coincalf.service.TurnoverOrderService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.annotation.Order;
import org.springframework.stereotype.Service;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 开仓订单信息 服务实现类
 * </p>
 *
 * @author qiang
 * @since 2018-05-12
 */
@Service
public class ForexOpenPositionOrderServiceImpl extends ServiceImpl<ForexOpenPositionOrderMapper, ForexOpenPositionOrder> implements ForexOpenPositionOrderService {

    @Autowired
    private MarketService marketService;

    @Autowired
    private TurnoverOrderService turnoverorderService;

    @Override
    public List<ForexOpenPositionOrderDTO> selectHoldingsDetails(long userId) {
        EntityWrapper<ForexOpenPositionOrder> ew = new EntityWrapper();
        ew.eq("user_id", userId);
        ew.eq("status", OpenPositionOrderStatus.OPEN.getCode());
        // 按创建时间倒叙
        ew.orderBy("created", false);
        List<ForexOpenPositionOrderDTO> forexOpenPositionOrderDTOS = new ArrayList<>();
        baseMapper.selectList(ew).forEach(f -> {
            ForexOpenPositionOrderDTO forexOpenPositionOrderDTO = FuForexOpenPositionOrderDTOMapper.INSTANCE.from(f);
            ForexMarketDTO forexMarket = marketService.getMarketDataById(f.getMarketId());
            Market market = marketService.getMarketById(f.getMarketId());
            //手续费
            TurnoverOrder turnoverorder = turnoverorderService.selectById(forexOpenPositionOrderDTO.getOrderId());
            BigDecimal opsitionGainAndLoss;
            if (forexOpenPositionOrderDTO.getType() == OrderType.BUY.getCode()) {
                forexOpenPositionOrderDTO.setFee(turnoverorder.getDealBuyFee());
                // 持仓盈亏
                opsitionGainAndLoss = forexMarket.getBidPrice().subtract(forexOpenPositionOrderDTO.getPrice())
                        .multiply(BigDecimal.valueOf(market.getContractUnit()))
                        .multiply(marketService.getPriceToCoinRate(market))
                        .multiply(forexOpenPositionOrderDTO.getNum());
            } else {
                forexOpenPositionOrderDTO.setFee(turnoverorder.getDealSellFee());
                // 持仓盈亏
                opsitionGainAndLoss = forexOpenPositionOrderDTO.getPrice().subtract(forexMarket.getAskPrice())
                        .multiply(BigDecimal.valueOf(market.getContractUnit()))
                        .multiply(marketService.getPriceToCoinRate(market))
                        .multiply(forexOpenPositionOrderDTO.getNum());
            }
            // 持仓盈亏
            forexOpenPositionOrderDTO.setOpsitionGainAndLoss(opsitionGainAndLoss.setScale(8, BigDecimal.ROUND_HALF_UP));
            // 现价
            forexOpenPositionOrderDTO.setCurrentPrice(forexMarket.getPrice());
            // 持仓量
            forexOpenPositionOrderDTO.setNum(forexOpenPositionOrderDTO.getNum().subtract(forexOpenPositionOrderDTO.getCloseNum()));
            forexOpenPositionOrderDTOS.add(forexOpenPositionOrderDTO);
        });
        return forexOpenPositionOrderDTOS;
    }
}
